package pub.guoxin.oops.manager.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.assertj.core.util.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.manager.service.RealTimeDeviceDataInfoService;
import pub.guoxin.oops.model.QRealTimeCommunication;
import pub.guoxin.oops.model.QRealTimeDeviceDataInfo;
import pub.guoxin.oops.model.RealTimeCommunication;
import pub.guoxin.oops.model.RealTimeDeviceDataInfo;
import pub.guoxin.oops.repository.RealTimeCommunicationRepository;
import pub.guoxin.oops.repository.RealTimeDeviceDataInfoRepository;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by guoxin on 18-1-31.
 */
@Service
public class RealTimeDeviceDataInfoServiceImpl implements RealTimeDeviceDataInfoService {
    @Autowired
    private RealTimeCommunicationRepository realTimeCommunicationRepository;
    @Autowired
    private RealTimeDeviceDataInfoRepository realTimeDeviceDataInfoRepository;

    @Override
    public Page<RealTimeDeviceDataInfo> schedule(String companyId, int pageNumber, int pageSize, String groupId) {
        // 查询所有通讯模块的id
        QRealTimeCommunication qRealTimeCommunication = QRealTimeCommunication.realTimeCommunication;
        BooleanExpression expression1 = qRealTimeCommunication.companyId.eq(companyId);
        if (!Strings.isNullOrEmpty(groupId)) {
            expression1 = expression1.and(qRealTimeCommunication.groupId.eq(groupId));
        }
        Iterable<RealTimeCommunication> all = realTimeCommunicationRepository.findAll(expression1);
        if (Iterables.isEmpty(all)) {
            return null;
        }
        List<String> ids = Arrays.stream(Iterables.toArray(all, RealTimeCommunication.class)).map(RealTimeCommunication::getId).collect(Collectors.toList());

        Pageable pageable = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "creation");
        QRealTimeDeviceDataInfo q = QRealTimeDeviceDataInfo.realTimeDeviceDataInfo;
        Page<RealTimeDeviceDataInfo> result = realTimeDeviceDataInfoRepository.findAll(q.realTimeCommunicationId.in(ids), pageable);
        return result;
    }

    @Override
    public boolean modifyRemark(String sn, String remark) {
        RealTimeDeviceDataInfo one = realTimeDeviceDataInfoRepository.findOne(sn);
        if (Objects.isNull(one)) {
            return false;
        }
        one.setRemark(remark);
        realTimeDeviceDataInfoRepository.save(one);
        return true;
    }

    @Override
    public Set<String> getDeviceSns(String groupId) {
        List<RealTimeCommunication> realTimeCommunications = realTimeCommunicationRepository.findByGroupId(groupId);
        Set<String> result = Sets.newHashSet();
        for (RealTimeCommunication realTimeCommunication : realTimeCommunications) {
            List<RealTimeDeviceDataInfo> realTimeDeviceDataInfos = realTimeDeviceDataInfoRepository.findByRealTimeCommunicationId(realTimeCommunication.getId());
            List<String> collect = realTimeDeviceDataInfos.stream().map(realTimeDeviceDataInfo -> realTimeCommunication.getId()).collect(Collectors.toList());
            result.addAll(collect);
        }
        return result;
    }

}
